package sdfi.lee.Filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet Filter implementation class MymakeFilter
 */
public class MymakeFilter implements Filter {

    /**
     * Default constructor. 
     */
    public MymakeFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here

		// pass the request along the filter chain
		HttpServletRequest hsr=(HttpServletRequest)request;
		HttpServletResponse hsresponse=(HttpServletResponse)response;
		HttpSession session=hsr.getSession();
		String jsp=hsr.getRequestURI();
		System.out.println(hsr.getContextPath());
		// project/news/newslist.jsp
		// news/newslist.jsp
		jsp=jsp.substring(jsp.indexOf("/")+1);
		if(jsp.contains("project"))
		{
			jsp=jsp.replaceAll("project", "");
		}
		if(session.getAttribute("userid")!=null&&session.getAttribute("username")!=null)
		{
			hsresponse.sendRedirect(hsr.getContextPath()+"/"+"mymake/"+jsp);
		}
		else
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
